package com.borrow.jiemihua.dialog.iosdialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.umeng.socialize.bean.SHARE_MEDIA;


/**
 * @author xuhui.han
 *         自定义的提示框
 */
public class ShareDialog {

    private Dialog dialog;
    private Context mContext;
    private ClickLinstener mLinstener;
    private TextView tv_qq;
    private TextView tv_qzon;
    private TextView tv_cancel;

    public ShareDialog(final Context mContext, ClickLinstener linstener) {
        dialog = new Dialog(mContext, R.style.custom_dialog);
        this.mContext = mContext;
        this.mLinstener = linstener;
        View view = View.inflate(mContext, R.layout.share_dialog, null);

        tv_qq = view.findViewById(R.id.tv_qq);
        tv_qzon = view.findViewById(R.id.tv_qzon);
        tv_cancel = view.findViewById(R.id.tv_cancel);

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLinstener.onShare(SHARE_MEDIA.QQ);
            }
        });
        tv_qzon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLinstener.onShare(SHARE_MEDIA.QZONE);
            }
        });

        dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        setDialogLocation(dialog, Gravity.CENTER, R.style.dialogWindowAnim);
        setDialogWidth(dialog, 20);//10这个值根据弹出框大小而定
    }

    /**
     * 创建dialog
     */
    public void show() {
        dialog.show();
    }

    public boolean isShowing() {
        return dialog.isShowing();
    }

    /**
     * 关闭dialog
     */
    public void dismissDialog() {
        dialog.dismiss();
    }

    public interface ClickLinstener {
        void onShare(SHARE_MEDIA shareMedia);
    }

    /**
     * 设置弹出框宽度
     *
     * @param dialog       对话框对象
     * @param marginValues 弹出框居左距右距离设定
     **/
    public  void setDialogWidth(Dialog dialog, int marginValues) {
        Window dialogWindow = dialog.getWindow();
        //获取屏幕大小
        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();//屏幕宽度
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.width = width - dp2px(marginValues * 2);//marginValues这个值设置越大。弹窗窗口越小。
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }

    /**
     * 设置弹出框位置
     **/
    public  void setDialogLocation(Dialog dialog, int location, Integer animation) {
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow == null) {
            return;
        }
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = location;
        dialogWindow.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        if (animation != null) {
            dialogWindow.setWindowAnimations(animation); //设置窗口弹出动画
        }
        dialogWindow.setAttributes(lp);
    }
    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public  int dp2px(final float dpValue) {
        final float scale =mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
